/* 
 * File:   CmsgLink.h
 * Author: root
 *
 * Created on May 20, 2019, 8:48 AM
 */

#ifndef CMSGLINK_H
#define	CMSGLINK_H

#include <pthread.h>

typedef void (*Callback)(void *arg, char *pData, int len);

typedef struct _list_element{
    int id; //内部消息识别ID
    int timeout; //等待超时重发时间（ms），0为不需要重发
    int pDataLen; //pData的长度
    void *pData;
    //===============
    void *arg;
    Callback callback;
}list_element;

typedef struct list_node{
    list_element element;
    struct list_node *pNext;
}list_node;

class CmsgLink {
public:
    
    CmsgLink();
    CmsgLink(const CmsgLink& orig);
    virtual ~CmsgLink();
    
    int getNodeNum(void);//获取有数据节点的个数
    int addNode(int id, int timeout, void *pData, int pDatalen);//创建链表子结点, return 0:suceed -1:failed
    int addNode(int id, int timeout, void *pData, int pDatalen, void *arg, Callback callback);//创建链表子结点, return 0:suceed -1:failed
    int deleteNode(int id);//删除链表子结点, return 0:suceed -1:failed, -2:id doesn't exit
    int tickNode(int passTime);
    int getTimeoutNodeData(void *pData, int &maxLen);
    int getTimeoutNodeData(void **pData);
    int getTimeoutNodeData(void **pData, void **arg, Callback *callback);

    int getNodeDataById(int id, void *pData, int maxLen);
    
    bool isNodeIdExist(int id);

private:

    int nodeNum;
    
    list_node *pNode; //链表头节点，此节点不放东西
    pthread_mutex_t mMutex;
    
    int createNode(void);//创建链表头结点, return 0:suceed -1:failed
    
};

#endif	/* CMSGLINK_H */

